/**
 * 
 */
package netPortfolioValue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author mingfan
 *
 */
public class YahooFinance {

	/**
	 * Retrieve quotation information from online finacial services
	 * 
	 * @param ticker
	 * @return
	 * @throws IOException
	 */
	public static YahooPriceRecord getPriceQuotation (final String ticker) throws IOException {
		
		final URL url = new URL("http://ichart.finance.yahoo.com/table.csv?s=" + ticker);
		
		try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
			in.readLine();
			final String quotation = in.readLine();
			final String[] quotationItems = quotation.split(",");
			return new YahooPriceRecord(quotationItems[0], 
										quotationItems[1],
										quotationItems[2], 
										quotationItems[3],
										quotationItems[4],
										quotationItems[5],
										quotationItems[6]);
		}
	}
	
	/**
	 * Just for test purpose
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main (String[] args) throws IOException {
		
		final URL url = new URL("http://ichart.finance.yahoo.com/table.csv?s=AAPL");
		
		final BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
		
		System.out.println(reader.readLine());
		System.out.println(reader.readLine());
	}
}
